<?php
class ConsoleAction extends Action {
	function _initialize() {
		if (! in_array ( strtolower ( ACTION_NAME ), array (
				'login',
				'checkloginfromoldside' 
		) )) {
			$s = session ( 'info.rid' );
			if (empty ( $s )) {
				// $this->redirect("/",array('err'=>'error03'));
				header ( 'location:/Admin/Console/login?err=error03' );
			}
		}
	}
	function login() {
		if (session ( "info" )) {
			$this->redirect ( "/Admin/Console/index" );
			return;
		}
		// t_admin
		$ob = M ( "csl_user" );
		
		if ($_POST) {
			
			if (session ( "verify" ) != md5 ( $_POST ['verifycode'] )) {
				$this->assign ( 'err', '验证码错误' );
			} else {
				$info = $ob->where ( array (
						'username' => $_POST ['name'] 
				) )->find ();
				if ($info ['password'] == md5 ( $_POST ['password'] . $info ['salt'] )) {
					session ( "info", $info );
					
					// 登录成功之后查询权限
					$ob = M ( "csl_role_auth" );
					$menuFirst = $ob->join ( "csl_menu m on m.menuid = csl_role_auth.menuid " )->field ( "m.menuid,m.name,m.pid,m.module,m.action,m.data,m.remark,m.ordid,m.display,m.moduleid" )->where ( array (
							"csl_role_auth.roleid" => $info ['rid'],
							"m.display" => "1",
							"m.pid" => "0" 
					) )->order ( "m.ordid" )->select ();
					
					$menuSecond = $ob->join ( "csl_menu m on m.menuid = csl_role_auth.menuid and m.display = '1' and m.pid != '0' " )->field ( "m.menuid,m.name,m.pid,m.module,m.action,m.data,m.remark,m.ordid,m.display,m.moduleid" )->where ( array (
							"csl_role_auth.roleid" => $info ['rid'],
							"m.display" => "1",
							"m.pid" => array (
									"neq",
									"0" 
							) 
					) )->order ( "m.ordid" )->select ();
					
					session ( "menuFirst", $menuFirst );
					session ( "menuSecond", $menuSecond );
					
					$this->assign ( "menuFirst", $menuFirst );
					$this->assign ( "menuSecond", $menuSecond );
					
					$or = M ( "csl_role" );
					$rolename = $or->where ( array (
							'roleid' => $info ['rid'] 
					) )->find ();
					session ( "rolename", $rolename ['rolename'] );
					
					$this->redirect ( "/Admin/Console/index" );
				} else {
					$this->assign ( 'err', '用户名密码错误' );
				}
			}
		}
		
		if ($_GET ['err'] == 'error03') {
			$this->assign ( 'err', '登录超时，请重新登录。' );
		}
		$this->display ( "Console/login" );
	}
	function index() {
		$this->assign ( "menuFirst", session ( "menuFirst" ) );
		$this->assign ( "menuSecond", session ( "menuSecond" ) );
		new SecurityAction ();
		
		$ob = M ( "csl_user" );
		$info = $ob->where ( array (
				'uid' => session ( 'info.uid' ) 
		) )->find ();
		
		$cash = M ( "csl_cash" );
		$bill = $cash->where ( array (
				'uid' => session ( 'info.uid' ) 
		) )->order ( 'id desc' )->select ();
		
		$this->assign ( 'info', $info );
		$this->assign ( 'bill', $bill );
		$this->display ( "Console/index" );
	}
	function logout() {
		session ( "info", null );
		session_destroy ();
		$this->redirect ( "/Admin/Console/login" );
	}
	function demo() {
		$this->display ( "Console/demo" );
	}
}
